package com.zmj.sy.mom.srv.aps.service;

import cn.hutool.core.collection.CollUtil;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ImportErpBom;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ImportErpMater;
import com.zmj.sy.mom.srv.aps.bean.entity.order.OrderAccessories;
import com.zmj.sy.mom.srv.aps.mapper.ImportErpBomMapper;
import com.zmj.sy.mom.srv.aps.mapper.ImportErpMaterMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class OrderAccessoriesService {

    private final ImportErpBomMapper importErpBomMapper;
    private final ImportErpMaterMapper importErpMaterMapper;

    @Transactional
    public List<OrderAccessories> fix(Long id) {
        ImportErpBom importErpBom = importErpBomMapper.selectById(id);

        List<ImportErpBom> all = new ArrayList<>();
        List<ImportErpBom> children = new ArrayList<>();
        children.add(importErpBom);

        // 遍历子节点
        while (CollUtil.isNotEmpty(children)) {
            all.addAll(children);

            List<ImportErpBom> allBomTree = importErpBomMapper.lambdaQuery()
                    .in(ImportErpBom::getPmCode, children.stream().map(ImportErpBom::getMaterialCode).distinct().collect(Collectors.toList()))
                    .eq(ImportErpBom::getErpOrderId, importErpBom.getErpOrderId())
                    .ne(ImportErpBom::getStatusId, "R")
                    .list();

            children = new ArrayList<>(allBomTree);
        }

        if (CollUtil.isEmpty(all)) {
            return new ArrayList<>();
        }

        List<ImportErpMater> materList = importErpMaterMapper.lambdaQuery()
                .in(ImportErpMater::getMaterialCode, all.stream().map(ImportErpBom::getMaterialCode).distinct().collect(Collectors.toList()))
                .eq(ImportErpMater::getErpOrderId, importErpBom.getErpOrderId())
                .list();

        Map<String, ImportErpMater> materMap = materList.stream().collect(Collectors.toMap(ImportErpMater::getMaterialCode, Function.identity(), (e1, e2) -> e1));

        List<OrderAccessories> collect = all.stream()
                .filter(e-> !e.getMaterialCode().matches("^(010|020|050|060).+$"))
                .map(e -> {
                    OrderAccessories r = new OrderAccessories();
                    r.setOrderId(null);
                    r.setBomId(null);
                    r.setPhaseId(null);
                    r.setSpec(e.getGraphNo());
                    r.setModel(e.getGrade());
                    r.setPartCount(new BigDecimal(e.getReqQty()));
                    r.setType(1);
                    r.setOrderType(1);
                    r.setBomPid(null);

//                    r.setFactoryCode(e.getFactory());
//                    r.setOrderCode(e.getOrderCode());
//                    r.setWorkOrderCode(e.getWorkOrderCode());
//                    r.setMaterCode(e.getMaterialCode());
//                    r.setMaterName(e.getMaterialName());
//                    r.setPerQty(e.getPerQty());
//                    r.setReqQty(e.getReqQty());
//                    r.setLgort(e.getLgort());
//                    r.setParentMaterCode(e.getPmCode());
//                    r.setCreateDateTime(e.getCreateDateTime());
//                    r.setStatusId(e.getStatusId());
//                    r.setPerReqQty(e.getPerReqQty());
//                    r.setPickQty(e.getPickQty());
//                    r.setPickCenter(e.getPickCenter());
//
//                    ImportErpMater importErpMater = materMap.get(e.getMaterialCode());
//                    if (importErpMater == null) {
//                        r.setDrawing(e.getGraphNo());
//                        r.setPlmId(e.getGraphNo());
//                        return r;
//                    }
//
//                    r.setPlmId(importErpMater.getPlmId());
                    return r;
                })
                .collect(Collectors.toList());

        return collect;
    }
}
